import { toJS } from "mobx";
import {
    observable,
    action
} from 'mobx';
import {
    WeaForm
} from 'comsMobx';
import {
    message
} from 'antd';

class BaseFormStore {
    @observable loading = false;
    @observable viewTop = false;    
    @observable routerParams = {};
    @observable form = {};
    @observable titleName = "";
    @observable titleIcon = "";
    @observable rightMenus = [];
    @observable formItemGroups = [];
    @observable subTables = [];
    @observable customDatas = {};

    getFormData(routerParams, componentInitCallBack){

    }


    @action
    componentWillMount=(componentParams)=>{
        this.routerParams = componentParams;
        this.form = new WeaForm();
        this.initData(componentParams);
    }

    @action
    initData(componentParams){

    }

    @action
    componentDidMount=()=>{
        if(this.viewTop){
            this.loading = true;
        }
        this.getFormData(this.routerParams, this.componentInitCallBack);
    }

    @action
    componentWillUnmount=()=>{
        if(this.viewTop){
            this.loading = false;
            this.titleName = "";
            this.titleIcon = "";
        }
        this.rightMenus = [];
        this.formItemGroups = [];
        this.routerParams = {};
        this.form = {};
        this.subTables = [];
        this.customDatas = {};
    }

    doSaveCheck=()=>{
        const requestParams = this.form.getFormParams();
        const formItemGroups = this.formItemGroups;
        let f = true;
        // 检查主表
        for (let i = 0; i < formItemGroups.length; i++) {
            let c = formItemGroups[i];
            let fields = c.items;
            for (let j = 0; j < fields.length; j++) {
                let viewAttr = fields[j].viewAttr;
                let fieldname = fields[j].domkey[0];
                let label = fields[j].label;
                let value = requestParams[fieldname];
                if (viewAttr == 3 && (value == "" || typeof(value) == "undefined")) {
                    f = false;
                    message.error("[" + label + "]为必填字段，不能为空！");
                    break;
                }
            }
            if (!f) {
                break;
            }
        }
        // 检查明细表
        if(f){
            const sts =  this.subTables;
            for(let i=0; i < sts.length; i++){
                const st = sts[i];
                const cde = st.columnDefine;
                const cda = st.columnData;                
                for(let j=0;j < cda.length; j++){                    
                    const rda = cda[j];
                    for(let k=0;k<cde.length;k++){
                        const label = cde[k].title;
                        const viewAttr = cde[k].viewAttr;
                        const dataIndex = cde[k].dataIndex;
                        const value = rda[dataIndex];
                        if(viewAttr == 3 && (value == "" || typeof(value) == "undefined")){
                            message.error("[" + label + "]为必填字段，不能为空！");
                            f = false;
                            break;
                        }
                    }
                    if(!f){
                        break;
                    }                    
                }
                if(!f){
                    break;
                }      
            }
        }
        if(f){
            if(this.viewTop){
                this.loading = true;
            }          
            requestParams.subTables = JSON.stringify(this.subTables); 
            this.doSave(this.routerParams,requestParams,this.doSaveCallBack);
        }
    }

    getFieldProps(fieldName){
        const formItemGroups = this.formItemGroups;
        for (let i = 0; i < formItemGroups.length; i++) {
            let c = formItemGroups[i];
            let fields = c.items;
            let finded = false;
            for (let j = 0; j < fields.length; j++) {
                if(fields[j].domkey[0] == fieldName){
                    return fields[j];
                    finded = true;
                    break;
                }
            }
            if(finded){
                break;
            }
        }
        return null;
    }

    getFieldValue(fieldName){
        const requestParams = this.form.getFormParams();
        for(let name in requestParams){
            if(fieldName == name){
                return requestParams[name];
            }
        }
    }

    @action
    doSave(routerParams,requestParams,doSaveCallBack){
        if(this.viewTop){
            this.loading = true;
        }
        this.doSaveData(routerParams,requestParams,doSaveCallBack);
    }

    @action
    doSaveCallBack=(data)=>{
        if(this.viewTop){
            this.loading = false;
        }
    }

    @action
    componentInitCallBack=(data)=>{
        const content = data.datas;
        this.rightMenus = content.rightMenus;
        this.formItemGroups = content.formItemGroups;
        this.subTables = content.subTables;
        this.customDatas = content.customDatas;
        if(this.viewTop){
            this.titleName = content.titleName;
            this.titleIcon = content.titleIcon;
            this.loading = false;
        }
        // 初始化表单组件
        this.form.initFormFields(content.formItemGroups);
    }

    formFieldUpdate(k,v){
        try {
            let fieldObj = {};
            fieldObj[k] = {value : v};
            this.form.updateFields(fieldObj,false);  
        } catch (error) {
            console.log("field ["+k+"] is not found from the form obj!");
        }
    }

    @action
    formFieldChange(fieldObj){

    }

    @action
    doSaveData(routerParams,requestParams,doSaveCallBack){

    }

    @action
    setSubTables(obj){
        this.subTables = obj;
    }
}

export default BaseFormStore;